package com.situ.day10;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Demo {
    @Test
    public void test1() {
        //运行时异常 RuntimeException

        //java.lang.ArithmeticException: / by zero
        int num = 3 / 0;

        String str = null;
        //NullPointerException
        str.length();
    }

    //编译时异常: 不是程序写的有问题，是未雨绸缪
    @Test
    public void test4() {
        //Unhandled exception: java.io.FileNotFoundException
        try {
            //try：可能抛出异常的代码
            FileInputStream fileInputStream = new FileInputStream("a.txt");
        } catch (FileNotFoundException e) {
            //catch: 如果抛出异常会被catch捕获
            throw new RuntimeException(e);
        } finally {
            //finally:不管抛不抛出异常，一定会执行的代码
            System.out.println("finally");
        }
    }

    @Test
    public void test5() {
        try {
            fun();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    //异常如果不去try-catch，可以选择throws抛出，但是谁调用谁要去解决
    //throws FileNotFoundException
    private void fun() throws FileNotFoundException {
        FileInputStream fileInputStream = new FileInputStream("a.txt");
        //ArithmeticException
    }


    //编译时异常继承自Exception
    //运行时异常继承自RuntimeException
    //ArithmeticException extends RuntimeException
}
